<?php $url = new Url();?>
<?php

class UserController extends Controller
{
    //public $layout='column1';
    //public $defaultAction = 'login';

    public function actionIndex(){    
        if(isset($_GET['tukhoa']))
        {
            $title = $_GET['tukhoa'];            
            $page = isset($_GET['page']) ? intval($_GET ['page']):1;            
            $status_user = $_GET['status_user'];            
            $tungay = DateUtils::convertDatedb($_GET['tungay']);            
            $denngay = DateUtils::convertDatedb($_GET['denngay']);
            //$this->pageTitle = "Trang sổ mơ";
            $tk = new AdminUser();
            $data = $tk->getRowsByTitleUser($title,$status_user,$tungay,$denngay);
            $count = count($data);
            $rows_per_page = 15;            
            if($count % $rows_per_page == 0)
            {
                $totalpage = floor($count/$rows_per_page);
            }
            else
            {
                $totalpage = floor($count/$rows_per_page) + 1;                             
            }

            $begin = ($page - 1)*$rows_per_page;
            //$end = $page * $rows_per_page;
            $end = $rows_per_page;
            $util = new Paging();
            $data = $tk->getRowsByTitleUserPaging($title,$status_user,$tungay,$denngay,$begin,$end);
            $get_url    = "?tukhoa=".$title."&status_user=".$status_user."&tungay=".$_GET['tungay']."&denngay=".$_GET['denngay'];
            $page       = $util->showPageNavigation($page,$totalpage,'index'.$get_url."&");
            //$page = $util->showPageNavigation($page,$totalpage,'index?');
            
            
            
            $this->render("index",array('data'=>$data,'page'=>$page,'begin'=>$begin,'tungay'=>$_GET['tungay'],'denngay'=>$_GET['denngay']));       
        }
        else
        {
            $page = isset($_GET['page']) ? intval($_GET ['page']):1;                        
            $tk = new AdminUser();
            $data = $tk->getRowsByUser();
            $count = count($data);
            $rows_per_page = 15;            
            if($count % $rows_per_page == 0)
            {
                $totalpage = floor($count/$rows_per_page);
            }
            else
            {
                $totalpage = floor($count/$rows_per_page) + 1;                             
            }            
            $begin = ($page - 1)*$rows_per_page;
            //$end = $page * $rows_per_page;            
            $end = $rows_per_page;            
            $util = new Paging();            
            $data = $tk->getRowsByUserPaging($begin,$end);            
            $page = $util->showPageNavigation($page,$totalpage,'index?');
            $this->render("index",array('data'=>$data,'page'=>$page,'begin'=>$begin));
        }
    }

    public function actionCreate(){        
        if(isset($_GET['edit']))
        {         
            $edit = 1;
            $id = $_GET['edit'];
            $model = new AdminUser();
            $data =  $model->getRowsById($id);               

            $this->render("create",array('edit'=>$edit,'data'=>$data));
        }
        else
        {
            $edit = 0;
            $this->render("create",array('edit'=>$edit));   
        }        
    }

    public function actionSubmitDelete(){                            
        $id = $_POST['id'];                                
        $model = new AdminUser();
        $data = $model->deleteRowById($id);
        if(count($data)==0)
        {
            echo 0;
        }
        else
        {
            echo 1;
        }
    }
    
    public function actionSubmitLock(){                            
        $id = $_POST['id'];                                
        $model = new AdminUser();
        $data = $model->updateStatusById($id,2);
        if(count($data)==0)
        {
            echo 0;
        }
        else
        {
            echo 1;
        }
    }

    public function actionSubmitEdit(){                            
        $id = $_POST['id'];                     
        $str = "create?edit=".$id;                        
        echo $str;        
    }  

    public function actionSubmitUpdate(){                 
        $username = $_POST['username'];                                
        $fullname = $_POST['fullname'];                                
        $email = $_POST['email'];               
        $mobile = $_POST['mobile'];               
        $address = $_POST['address'];               
        $xu = $_POST['xu'];               
        $id = $_POST['id'];               
        $model = new AdminUser();
        $data = $model->updateRowById($id,$username,$fullname,$email,$mobile,$address,$xu);
        if(count($data)==0)
        {
            echo 0;
        }
        else
        {
            echo 1;
        }
    } 

    public function actionSubmitInsert(){   
        $username = $_POST['username'];                                
        $fullname = $_POST['fullname'];                                
        $email = $_POST['email'];               
        $mobile = $_POST['mobile'];               
        $address = $_POST['address'];               
        $xu = $_POST['xu'];                               
        $password = md5($_POST['password']);        
        $model = new AdminUser();
        $check =  $model->getRowsByTitleUser($username);
        if(count($check)==1)
        {
            echo 2;die;
        }
        $data = $model->insertRow($username,$fullname,$email,$mobile,$address,$xu,$password);        
        if(count($data)==0)
        {
            echo 0;
        }
        else
        {
            echo 1;
        }
    }    

    public function actionSubmitUpdateStatus(){                     
        $status = $_POST['active'];               
        $id = $_POST['id'];                       
        $model = new AdminUser();
        $data = $model->updateStatusById($id,$status);
        if(count($data)==0)
        {
            echo 0;
        }
        else
        {
            echo 1;
        }
    }  

    /*Trang lịch sử giao dịch*/
    public function actionLichsu(){
        $url = new Url();
        $key_word = isset($_GET['key_word'])? $_GET['key_word']   :"";
        /*$list_user = AdminUser::getRowByKeyWord($key_word);*/

        $type       = isset($_GET['type'])         ? intval($_GET['type'])        :   0;
        $sub_type   = isset($_GET['sub_type'])     ? intval($_GET['sub_type'])    :   0;
        $fromDate   = isset($_GET['from'])         ? $_GET['from']                :   date("d/m/Y",strtotime("-7 days"));
        $toDate     = isset($_GET['to'])           ? $_GET['to']                  :   date("d/m/Y",(time()));

        $tong_nap   = 0;
        $tong_tieu  = 0; 
        /*foreach($list_user as $key => $value){
        $arr_id .= $value['id'].",";
        $arr_user[$value['id']] = $value['username'];       
        }*/ 
        /*$arr_id = rtrim($arr_id,",");*/

        $fromDate_db    = date("Y-m-d H:i:s",strtotime(DateUtils::convertDatedb($fromDate))) ;
        $toDate_db      = date("Y-m-d H:i:s",(strtotime(DateUtils::convertDatedb($toDate))+86300));

        switch($type){
            case 0:
                $in     = AdminTransactionIn::getAllRowInDate($fromDate_db,$toDate_db,$sub_type,$key_word);
                $out    = AdminTransactionOut::getAllRowInDate($fromDate_db,$toDate_db,$sub_type,$key_word);

                foreach($in as $key=>$value){
                    $tong_nap += $value['nap_tien'];    
                }

                foreach($out as $key=>$value){
                    $tong_tieu += $value['tru_tien'];    
                }
                $data = $in;
                $count = count($in);
                foreach($out as $key=>$value){          
                    $data[$count] = $value; 
                    $count++;  
                }
                $count_data = count($data);
                for($i = 0; $i < $count_data; $i++){
                    for($j = 0; $j < $count_data - 1 -$i; $j++){
                        if($data[$j+1]['ngay'] > $data[$j]['ngay']){
                            $tmp1 = $data[$j];
                            $tmp2 = $data[$j+1];
                            $data[$j] = $tmp2;
                            $data[$j+1] = $tmp1;                                     
                        }
                    }
                }

                break;

            case 1:
                $out  = AdminTransactionOut::getAllRowInDate($fromDate_db,$toDate_db,$sub_type,$key_word); 
                foreach($out as $key=>$value){
                    $tong_tieu += $value['tru_tien'];    
                }
                $data = $out;
                break;

            case 2:
                $in   = AdminTransactionIn::getAllRowInDate($fromDate_db,$toDate_db,$sub_type,$key_word);
                foreach($in as $key=>$value){
                    $tong_nap += $value['nap_tien'];    
                }
                $data = $in;
                break;
        }
        //echo $type;die;
        /*echo "<pre>";
        print_r($data);die;*/

        /*Phân trang*/
        $page = isset($_GET['page'])? intval($_GET['page']):1;
        $count_row = count($data);

        $rows_per_page = 20;   
        $begin = ($page - 1)*$rows_per_page;                        
        $end = $page*$rows_per_page;



        if($count_row % $rows_per_page == 0){
            $total_page = floor($count_row/$rows_per_page);
        } else{
            $total_page = floor($count_row/$rows_per_page) + 1;                             
        }
        /*echo "<pre>";
        print_r($data);die; */
        //echo $begin."==".$end;die;
        $temp = array();
        $j = 0;
        for($i = $begin; $i < $end; $i++ ){
            if(isset($data[$i])){
                $temp[$j] = $data[$i]; 
                $j++;  
            }
        }
        /*echo "<pre>";
        print_r($temp);die;*/ 
        $data = $temp; 
        $get_url = "?key_word=$key_word&type=$type&sub_type=$sub_type&from=$fromDate&to=$toDate";
        $page = Paging::showPageNavigationMore($page,$total_page,$url->createUrl('user/lichsu').$get_url."&"); 
        $this->render('history',array(
        "data"=>$data,
        "fromDate"=>$fromDate,
        "toDate"=>$toDate,
        "type"=>$type,
        "sub_type"=>$sub_type,
        "tong_nap"=>$tong_nap,
        "tong_tieu"=>$tong_tieu,

        "page"=>$page,
        "begin"=>$begin,
        "key_word"=>$key_word));

    } 

    /*Ajax load hình thức nạp/tiêu tiền*/
    public function actionAjaxLoadSubType(){
        $arrTypeIn = LoadConfig::$arrayTypeTransactionIn;
        $arrTypeOut = LoadConfig::$arrayTypeTransactionOut;

        $type_id = isset($_POST['type_id']) ? intval($_POST['type_id']) : 0;

        $html = "";
        switch($type_id){
            case 0:
                $html = "<option value='0'>Tất cả</option>";
                break;

            case 1:
                $html = "<option value='0'>Tất cả</option>";
                foreach($arrTypeOut as $key => $value){
                    $html .= "<option value='$key'>$value</option>";
                }
                break;

            case 2:
                $html = "<option value='0'>Tất cả</option>";
                foreach($arrTypeIn as $key => $value){
                    $html .= "<option value='$key'>$value</option>";
                }
                break;
        }
        echo $html;
    }  
}
